<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.question"
            placeholder="输入您的问题"
            clearable
            style="width: 300px"
          />

          <el-button
            type="primary"
            round
            icon="el-icon-search"
            @click="getTbQuestionList"
          >生成
          </el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="tbQuestionList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="question" label="问题" width="350"/>
        <el-table-column prop="answer" label="回答" width="780"/>
        <el-table-column label="重试" width="50">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-video-play" size="mini" circle @click="refresh(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column label="刷新" width="50">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-refresh" size="mini" circle @click="getSingleQuestion(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50,100,200,500]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import aiApi from '@/api/tbAi'

export default {
  created() {
    this.getTbQuestionList()
  },
  mounted() {
    this.searchModel.name = this.name // 将获取到的 name 值赋给 userName
  },
  data() {
    return {
      formLabelWidth: '130px',
      dialogFormVisible: false,
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
        question: ''
      },
      tbQuestionList: []
    }
  },
  methods: {
    clearForm() {
      this.userForm = {
        // coupon: '0**0'
      }
    },
    getTbQuestionList() {
      aiApi.getTbQuestionList(this.searchModel).then((response) => {
        this.tbQuestionList = response.data.rows
        this.total = response.data.total
      })
    },
    refresh(ques) {
      aiApi.getRefresh(ques.question).then((res) => {
        this.$message({
          type: 'success',
          message: res.message
        })
        this.getSingleQuestion(ques)
      })
    },
    getSingleQuestion(ques) {
      aiApi.getSingleQuestion(ques.question).then((res) => {
        this.tbQuestionList = res.data.rows
        this.total = res.data.total
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}

.blue-link {
  color: blue;
}

</style>
